/*** 
 * @Author: Colin Dai
 * @Date: 2022-04-30 09:01:33
 * @LastEditors: Colin Dai
 * @LastEditTime: 2022-04-30 09:13:17
 * @FilePath: \intelligent-trash\src\ai-audio.h
 * @Description: AI语音交互相关逻辑
 * @dailianzheng@163.com
 * @Copyright (c) 2022 by Colin.Dai, All Rights Reserved. 
 */

#include "ai-audio.h"
#include <SoftwareSerial.h>


/**
 * @brief 全局变量定义
 * 
 */
#define RX_PIN     2
#define TX_PIN     3


SoftwareSerial audio_serial(RX_PIN,TX_PIN);

/*** 
 * @brief 初始化AI语音模块
 * @param {*}
 * @return {*}
 */
void init_AI_audio()
{
    audio_serial.begin(9600);

    Serial.println("init_AI_audio...");
    
}

/*** 
 * @brief 检查语音指令
 * @param {*}
 * @return {AUDIO_RECV_CMD_TYPE} 指令枚举
 */
AUDIO_RECV_CMD_TYPE check_audio_cmd()
{
    //默认无指令
    AUDIO_RECV_CMD_TYPE cmd = CMD_R_A_NULL;

    while(audio_serial.available() > 0)
    {
        //每次只读最后一次的指令
        cmd =  (AUDIO_RECV_CMD_TYPE)audio_serial.read();
    }

    return cmd;
}

/**
 * @brief 发送语音指令
 * @param {AUDIO_SEND_CMD_TYPE} cmd
 * @return {*}
 */
void send_audio_cmd(AUDIO_SEND_CMD_TYPE cmd)
{
    //目前固定格式
    unsigned char cmdbuff[5]={0xAA,0x55,0xFF,0x55,0xAA};
    
    //通道号
    cmdbuff[2] = cmd;

    for(int i = 0; i< 5;i++)
    {
        audio_serial.write(cmdbuff[i]);
        Serial.print(cmdbuff[i],HEX);       
    }

    Serial.println();

    Serial.println("send_audio_cmd...");
}

